package org.example.fastproject.web.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.fastproject.core.bean.PageParam;
import org.example.fastproject.web.sys.model.SysLog;
import org.example.fastproject.web.sys.mapper.SysLogMapper;
import org.example.fastproject.web.sys.model.vo.log.LogVo;
import org.example.fastproject.web.sys.service.SysLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 日志表 服务实现类
 * </p>
 *
 * @author 王海舟
 * @since 2023-01-20 15:38:29
 */
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements SysLogService {

    @Override
    @Async
    public void saveAsync(SysLog sysLog) {
        this.save(sysLog);
    }

    @Override
    public Page<LogVo> page(PageParam param) {
        Page page = new Page(param.getNum(),param.getSize());
        this.page(page);
        List<SysLog> list = page.getRecords();
        List<LogVo> logs = list.stream().map(sysLog -> {
            return BeanUtil.toBean(sysLog, LogVo.class);
        }).collect(Collectors.toList());
        page.setRecords(logs);
        return page;
    }
}
